package com.kye.express.business.customer_code.customer_code_manager;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.customer_code.bind_customer_code.BindCustomerCodeActivity;
import com.kye.express.business.customer_code.modify_customer_password.ModifyCustomerPasswordActivity;
import com.kye.express.databinding.ActivityCustomerCodeManagerBinding;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.user.User;

import io.reactivex.functions.Consumer;

public class CustomerCodeManagerActivity extends BaseToolbarActivity implements CustomerCodeManagerContract.CustomerCodeManagerView {

    private ActivityCustomerCodeManagerBinding mBinding;
    private CustomerCodeManagerContract.CustomerCodeManagerPresenter mPresenter;
    private Dialog mDialog;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, CustomerCodeManagerActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_customer_code_manager);

        initEvent();
        initData();
    }

    private void initEvent() {
        setUpToolbar(mBinding.toolbar);

        RxView.clicks(mBinding.linearLayoutBindCustomerCode)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        BindCustomerCodeActivity.startActivity(CustomerCodeManagerActivity.this);
                    }
                });

        RxView.clicks(mBinding.linearLayoutModifyCustomerPassword)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        ModifyCustomerPasswordActivity.startActivity(CustomerCodeManagerActivity.this);
                    }
                });

        RxView.clicks(mBinding.buttonUnbindCustomerCode)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {

                    private MaterialDialog mDialog;

                    @Override
                    public void accept(Object o) throws Exception {
                        if (mDialog == null) {
                            mDialog = new MaterialDialog.Builder(CustomerCodeManagerActivity.this)
                                    .content(R.string.customer_code_manager_confirm_unbind_customer_code)
                                    .positiveText(R.string.common_confirm)
                                    .negativeText(R.string.common_cancel)
                                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            mPresenter.unbindCustomerCode(mBinding.getUser().getPhone(), mBinding.getUser().getCustomCode());

                                            dialog.dismiss();
                                        }
                                    })
                                    .build();
                        }
                        mDialog.show();
                    }
                });

    }

    private void initData() {
        mPresenter = new CustomerCodeManagerContract.CustomerCodeManagerPresenter();
        mPresenter.bind(this);
        mPresenter.getUser();
        mPresenter.checkAuthorizedByUpdateCustomPassword();
        mPresenter.getCompanyName();
    }

    @Override
    public void onUser(User user) {
        mBinding.setUser(user);
    }

    @Override
    public void onSuccess() {
        BindCustomerCodeActivity.startActivity(this);
        finish();
    }

    @Override
    public void onAuthorized(boolean authorized) {
        mBinding.setAuthorized(authorized);
    }

    @Override
    public void onCompanyName(String s) {
        mBinding.setCompanyName(s);
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }

    @Override
    public void onLoading() {
        if (mDialog == null) {
            mDialog = new KyeProgressDialog.Builder(this).build();
        }
        mDialog.show();
    }

    @Override
    public void onLoaded() {
        mDialog.dismiss();
    }
}
